﻿#region 信息

// Copyright©2014 Rex Rao all rights reserved.
// File name : SoMcCoffee\Sohobloo.SoPermission.Data\Configuration.cs
// Author : Sohobloo
// Create time : 2014/01/31 23:40
// Update time : 2014/02/05 9:01

#endregion

#region 引用

using System;
using System.Collections.ObjectModel;
using System.Data.Entity.Migrations;
using Sohobloo.SoCommon.Security;
using Sohobloo.SoMcCoffee.Data;
using Sohobloo.SoPermission.Common;
using Sohobloo.SoPermission.Entity;

#endregion

namespace Sohobloo.SoPermission.Data.Migrations
{
    internal sealed class Configuration : DbMigrationsConfiguration<EfDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
            AutomaticMigrationDataLossAllowed = true;
        }

        protected override void Seed(EfDbContext context)
        {
            var now = DateTime.UtcNow;
            const string dbOperator = "System";
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
            var superAdminGroup = new Group
            {
                Id = new Guid("00000000-0000-0001-0000-000000000001"),
                Name = "SuperAdmin",
                Description = "超级管理员",
                Enabled = true,
                CreateTime = new DateTime(1900, 1, 1),
                CreateUser = dbOperator,
                UpdateTime = now,
                UpdateUser = dbOperator
            };
            var superAdminAccount = new Account
            {
                Id = new Guid("00000000-0000-0002-0000-000000000001"),
                UserName = "administrator",
                PasswordHash = Md5.GetSaltedMd5("administrator"),
                NickName = "超级管理员",
                Email = "administrator@sohobloo.com",
                AccountStatus = AccountStatus.Normal,
                CreateTime = new DateTime(1900, 1, 1),
                CreateUser = dbOperator,
                UpdateTime = now,
                UpdateUser = dbOperator
            };
            superAdminGroup.Accounts = new Collection<Account> {superAdminAccount};
            var roleSet = context.Set<Group>();
            roleSet.AddOrUpdate(superAdminGroup);
            context.SaveChanges();
        }
    }
}